﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models.Users;
using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models.NewsModel;

namespace Think.Lenovo.Com.DataAccess.NewsInfo
{
    public class NewsInfo
    {
        /// <summary>
        /// 添加一条新闻
        /// </summary>
        /// <param name="newsModel"></param>
        /// <returns></returns>
        public long AddNews(NewsModel newsModel)
        {
            #region //信息

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into ")
                .Append("tbl_news")
                .Append("(Title, ABBTitle, TuiJian, TiaoZhuan, ")
                .Append("JiaCu, TouTiao, Tree, source,")
                .Append(" Author, Target, Taxis, AbbPic,")
                .Append("Keyword, Abstracts, Content, Feedback, ")
                .Append("Sign, BrowseRight, BrowseTime,ReleaseDate,Integral,AddUserId,AreaId,types)")

                .Append("values")

                .Append("(@Title, @ABBTitle, @TuiJian, @TiaoZhuan, ")
                .Append("@JiaCu, @TouTiao, @Tree, @source, ")
                .Append("@Author, @Target, @Taxis, @AbbPic,")
                .Append("@Keyword, @Abstracts, @Content, @Feedback, ")
                .Append("@Sign, @BrowseRight,@BrowseTime,@ReleaseDate,@Integral,@AddUserId,@AreaId,@types)");

            #endregion

            #region//Sql参数数据
            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Title",MySqlDbType.VarChar,50),
                    new MySqlParameter("@ABBTitle",MySqlDbType.VarChar,50),
                    new MySqlParameter("@TuiJian",MySqlDbType.Int32),
                    new MySqlParameter("@TiaoZhuan",MySqlDbType.Int32),
                    new MySqlParameter("@JiaCu",MySqlDbType.Int32),
                    new MySqlParameter("@TouTiao",MySqlDbType.Int32),
                    new MySqlParameter("@Tree",MySqlDbType.Int32),
                    new MySqlParameter("@source",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Author",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Target",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Taxis",MySqlDbType.VarChar,200),
                    new MySqlParameter("@AbbPic",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Keyword",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Abstracts",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Content",MySqlDbType.Text),
                    new MySqlParameter("@Feedback",MySqlDbType.Int32),
                    new MySqlParameter("@Sign",MySqlDbType.Int32),
                    new MySqlParameter("@BrowseRight",MySqlDbType.VarChar,200),
                    new MySqlParameter("@BrowseTime",MySqlDbType.Int32),
                    new MySqlParameter("@ReleaseDate",MySqlDbType.DateTime),
                    new MySqlParameter("@Integral",MySqlDbType.Int32),
                    new MySqlParameter("@AddUserId",MySqlDbType.Int32),
                    new MySqlParameter("@AreaId",MySqlDbType.Int32),
                    new MySqlParameter("@types",MySqlDbType.Int32)
                };

            ps[0].Value = newsModel.Title;
            ps[1].Value = newsModel.ABBTitle;
            ps[2].Value = newsModel.TuiJian;
            ps[3].Value = newsModel.TiaoZhuan;
            ps[4].Value = newsModel.JiaCu;
            ps[5].Value = newsModel.TouTiao;
            ps[6].Value = newsModel.Tree;
            ps[7].Value = newsModel.Source;
            ps[8].Value = newsModel.Author;
            ps[9].Value = newsModel.Target;
            ps[10].Value = newsModel.Taxis;
            ps[11].Value = newsModel.AbbPic;
            ps[12].Value = newsModel.Keyword;
            ps[13].Value = newsModel.Abstracts;
            ps[14].Value = newsModel.Content;
            ps[15].Value = newsModel.Feedback;
            ps[16].Value = newsModel.Sign;
            ps[17].Value = newsModel.BrowseRight;
            ps[18].Value = newsModel.BrowseTime;
            ps[19].Value = newsModel.ReleaseDate;
            ps[20].Value = newsModel.Integral;
            ps[21].Value = newsModel.AddUserId;
            ps[22].Value = newsModel.AreaId;
            ps[23].Value = newsModel.Types;
            #endregion
            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);

            sql.Remove(0, sql.Length);
            if(newsModel.UserRole==null || newsModel.UserRole!=1)
            {
                  sql.Append("insert into tbl_news_state(newsid,state) values(@newsid,0)");
            }
            else
            {
                sql.Append("insert into tbl_news_state(newsid,state) values(@newsid,1)");
            }
              
            MySqlParameter[] psstate = new MySqlParameter[]
                                           {
                                               new MySqlParameter("@newsid", MySqlDbType.Int32)
                                           };
            psstate[0].Value = (int) id;
            MysqlHelper.Insert(conn, sql.ToString(), psstate);
            return id;

            #endregion
        }

        /// <summary>
        /// 修改一条新闻
        /// </summary>
        /// <param name="newsModel"></param>
        /// <returns></returns>
        public long UpdateNews(NewsModel newsModel)
        {
            long id = 0;
            try
            {
                #region //信息

                String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

                #region//sql语句
                StringBuilder sql = new StringBuilder();
                sql.Append(" Update ")
                    .Append(" tbl_news ")
                    .Append(" Set Title=@Title, ABBTitle=@ABBTitle,")
                    .Append("TuiJian=@TuiJian, TiaoZhuan=@TiaoZhuan, ")
                    .Append("JiaCu=@JiaCu, TouTiao=@TouTiao,")
                    .Append("Tree=@Tree, source=@source,")
                    .Append(" Author=@Author, Target= @Target,")
                    .Append("Taxis=@Taxis, AbbPic=@AbbPic,")
                    .Append("Keyword=@Keyword, Abstracts=@Abstracts,")
                    .Append("Content=@Content, Feedback=@Feedback, ")
                    .Append("Sign=@Sign, ")
                    .Append("BrowseRight=@BrowseRight, BrowseTime=@BrowseTime,ReleaseDate=@ReleaseDate,Integral=@Integral")
                    .Append(" Where Id=@Id");
                //,AddUserId=@AddUserId,AreaId=@AreaId 
                #endregion

                #region//Sql参数数据
                MySqlParameter[] ps = new MySqlParameter[] {
                new MySqlParameter("@Id",MySqlDbType.Int32),
                    new MySqlParameter("@Title",MySqlDbType.VarChar,50),
                    new MySqlParameter("@ABBTitle",MySqlDbType.VarChar,50),
                    new MySqlParameter("@TuiJian",MySqlDbType.Int32),
                    new MySqlParameter("@TiaoZhuan",MySqlDbType.Int32),
                    new MySqlParameter("@JiaCu",MySqlDbType.Int32),
                    new MySqlParameter("@TouTiao",MySqlDbType.Int32),
                    new MySqlParameter("@Tree",MySqlDbType.Int32),
                    new MySqlParameter("@source",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Author",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Target",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Taxis",MySqlDbType.Int32),
                    new MySqlParameter("@AbbPic",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Keyword",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Abstracts",MySqlDbType.VarChar,200),
                    new MySqlParameter("@Content",MySqlDbType.Text),
                    new MySqlParameter("@Feedback",MySqlDbType.Int32),
                    new MySqlParameter("@Sign",MySqlDbType.Int32),
                    new MySqlParameter("@BrowseRight",MySqlDbType.VarChar,200),
                    new MySqlParameter("@BrowseTime",MySqlDbType.Int32),
                    new MySqlParameter("@ReleaseDate",MySqlDbType.DateTime),
                    new MySqlParameter("@Integral",MySqlDbType.Int32)
                    //new MySqlParameter("@AddUserId",MySqlDbType.Int32),
                    //new MySqlParameter("@AreaId",MySqlDbType.Int32)
                };
                ps[0].Value = newsModel.Id;
                ps[1].Value = newsModel.Title;
                ps[2].Value = newsModel.ABBTitle;
                ps[3].Value = newsModel.TuiJian;
                ps[4].Value = newsModel.TiaoZhuan;
                ps[5].Value = newsModel.JiaCu;
                ps[6].Value = newsModel.TouTiao;
                ps[7].Value = newsModel.Tree;
                ps[8].Value = newsModel.Source;
                ps[9].Value = newsModel.Author;
                ps[10].Value = newsModel.Target;
                ps[11].Value = newsModel.Taxis;
                ps[12].Value = newsModel.AbbPic;
                ps[13].Value = newsModel.Keyword;
                ps[14].Value = newsModel.Abstracts;
                ps[15].Value = newsModel.Content;
                ps[16].Value = newsModel.Feedback;
                ps[17].Value = newsModel.Sign;
                ps[18].Value = newsModel.BrowseRight;
                ps[19].Value = newsModel.BrowseTime;
                ps[20].Value = Convert.ToDateTime(newsModel.ReleaseDate);
                ps[21].Value = newsModel.Integral;
                //ps[22].Value = newsModel.AddUserId;
                //ps[23].Value = newsModel.AreaId;
                #endregion
                id = MysqlHelper.Update(conn, sql.ToString(), ps);
            }
            catch (Exception ex)
            {
                throw;
            }
            return id;

                #endregion
        }

        /// <summary>
        /// 删除第一条信息
        /// </summary>
        /// <param name="id"></param>
        public void DeleteNews(int id)
        {
            #region //信息

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            string sql = "delete from tbl_news Where Id=@Id";

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@id",MySqlDbType.Int32)
            };
            ps[0].Value = id;
            MysqlHelper.Delete(conn, sql.ToString(), ps);
            #endregion
            #endregion
        }
        /// <summary>
        /// 更新资讯状态
        /// </summary>
        /// <param name="id"></param>
        public void UpDataNewsState(string id,int state)
        {
            #region //信息

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            string sql = "update tbl_news_state set state="+state+" where newsid in ("+id+") ";

          
            MysqlHelper.Delete(conn, sql.ToString(), null);
            #endregion
            #endregion
        }

        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页多少数据</param>
        /// <returns></returns>
        public LinkedList<NewsModel> GetViewNews(int pageIndex, int pageSize)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            sql.Append("Select * from tbl_news limit ")
                .Append((pageIndex - 1) * pageSize)
                .Append(",")
                .Append(pageSize);
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            LinkedList<NewsModel> list = new LinkedList<NewsModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        DateTime dt = DateTime.Now;
                        Int32 i;
                        Decimal d;
                        NewsModel model = new NewsModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Title = Convert.ToString(row["title"]);
                        model.ABBTitle = Convert.ToString(row["ABBTitle"]);
                        model.Types = Convert.ToInt32(row["Types"]);
                        model.Source = Convert.ToString(row["source"]);
                        model.Author = Convert.ToString(row["author"]);
                        model.Target = Convert.ToString(row["target"]);
                        model.Taxis = Convert.ToInt32(row["taxis"]);
                        model.AbbPic = Convert.ToString(row["abbPic"]);
                        model.Keyword = Convert.ToString(row["keyword"]);
                        model.Abstracts = Convert.ToString(row["abstracts"]);

                        model.Content = Convert.ToString(row["Content"]);
                        model.Feedback = Convert.ToInt32(row["feedback"]);
                        model.Sign = Convert.ToInt32(row["sign"]);
                        model.BrowseRight = Convert.ToString(row["browseRight"]);

                        model.BrowseTime = Convert.ToInt32(row["browsetime"]); ;
                        DateTime.TryParse(row["releaseDate"].ToString(), out dt);
                        model.ReleaseDate = dt;

                        Int32.TryParse(row["integral"].ToString(), out i);
                        model.Integral = i;
                        model.TuiJian = Convert.ToInt32(row["TuiJian"]);
                        model.TiaoZhuan = Convert.ToInt32(row["TiaoZhuan"]);
                        model.JiaCu = Convert.ToInt32(row["JiaCu"]);
                        model.TouTiao = Convert.ToInt32(row["TouTiao"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 得到所有的新闻
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllNews()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select * from tbl_news   where types=1 order by taxis asc";
            DataSet ds = new DataSet();
            ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            return ds;
        }
        /// <summary>
        /// 得到新闻数量
        /// </summary>
        /// <returns></returns>
        public int GetNewsCount()
        {
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select count(Id) From ")
                .Append(" tbl_news ");
            #endregion

            Object obj = MysqlHelper.ExecuteScalar(conn, sql.ToString());
            return obj == null ? 0 : Convert.ToInt32(obj);

            #endregion
        }

        public int GetNewsCountByTypes(int id)
        {
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select count(*) from tbl_news where types =" + id);
            #endregion

            Object obj = MysqlHelper.ExecuteScalar(conn, sql.ToString());
            return obj == null ? 0 : Convert.ToInt32(obj);

            #endregion

            
        }

        /// <summary>
        /// 根据一个新闻id 得到他的信息
        /// </summary>
        /// <param name="userId">新闻Id</param>
        /// <returns>新闻信息实体</returns>
        public NewsModel GetNewsInfo(int NewsId)
        {
            NewsModel model = new NewsModel();
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select * From ")
                .Append(" tbl_News ")
                .Append(" Where Id=@Newsid");
            #endregion

            #region//Sql参数数据
            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Newsid",MySqlDbType.Int32)
                };
            ps[0].Value = NewsId;
            #endregion

            DataSet ds = MysqlHelper.Query(conn, sql.ToString(), ps);
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.Title = Convert.ToString(ds.Tables[0].Rows[0]["Title"]);
                        model.ABBTitle = Convert.ToString(ds.Tables[0].Rows[0]["ABBTitle"]);
                        model.Abstracts = Convert.ToString(ds.Tables[0].Rows[0]["Abstracts"]);
                        model.Author = Convert.ToString(ds.Tables[0].Rows[0]["Author"]);
                        model.BrowseRight = Convert.ToString(ds.Tables[0].Rows[0]["BrowseRight"]);
                        model.BrowseTime = Convert.ToInt32(ds.Tables[0].Rows[0]["BrowseTime"]);
                        model.Content = Convert.ToString(ds.Tables[0].Rows[0]["Content"]);
                        model.Feedback = Convert.ToInt32(ds.Tables[0].Rows[0]["Feedback"]);
                        model.Integral = Convert.ToInt32(ds.Tables[0].Rows[0]["Integral"]);
                        model.JiaCu = Convert.ToInt32(ds.Tables[0].Rows[0]["JiaCu"]);
                        model.Keyword = Convert.ToString(ds.Tables[0].Rows[0]["Keyword"]);
                        model.ReleaseDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["ReleaseDate"]);
                        model.Sign = Convert.ToInt32(ds.Tables[0].Rows[0]["Sign"]);
                        model.Source = Convert.ToString(ds.Tables[0].Rows[0]["source"]);
                        model.Target = Convert.ToString(ds.Tables[0].Rows[0]["Target"]);
                        model.Taxis = Convert.ToInt32(ds.Tables[0].Rows[0]["Taxis"]);
                        model.TiaoZhuan = Convert.ToInt32(ds.Tables[0].Rows[0]["TiaoZhuan"]);
                        model.TouTiao = Convert.ToInt32(ds.Tables[0].Rows[0]["TouTiao"]);
                        //model.Tree = Convert.ToInt32(ds.Tables[0].Rows[0]["Tree"]);
                        model.TuiJian = Convert.ToInt32(ds.Tables[0].Rows[0]["TuiJian"]);
                        model.Types = Convert.ToInt32(ds.Tables[0].Rows[0]["types"]);
                        model.AbbPic = Convert.ToString(ds.Tables[0].Rows[0]["abbpic"]);
                    }
                }
            }
            return model;
            #endregion
        }

        public LinkedList<NewsModel> GetViewNews(int pageIndex, int pageSize, String conditions, String orderBy)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            sql.Append("Select a.*,(case when b. state is null then 0 else b.state end) as state,realName from tbl_news as a left join tbl_news_state as b on a.id=b.newsid ");
            sql.Append(" inner join tbl_userinfo as c  on a.addUserId=c.userid ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(" Where " + conditions + " ");
            }
            if (!String.IsNullOrEmpty(orderBy))
            {
                sql.Append(" Order By " + orderBy + "  limit ");
            }
            sql.Append((pageIndex - 1) * pageSize)
            .Append(",")
            .Append(pageSize);
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            LinkedList<NewsModel> list = new LinkedList<NewsModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        DateTime dt = DateTime.Now;
                        Int32 i;
                        Decimal d;
                        NewsModel model = new NewsModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Title = Convert.ToString(row["title"]);
                        model.ABBTitle = Convert.ToString(row["ABBTitle"]);
                        if (!Int32.TryParse(row["Types"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Types = i;
                        model.Source = Convert.ToString(row["source"]);
                        model.Author = Convert.ToString(row["author"]);
                        model.Target = Convert.ToString(row["target"]);
                        if (!Int32.TryParse(row["taxis"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Taxis = i;
                        model.AbbPic = Convert.ToString(row["abbPic"]);
                        model.Keyword = Convert.ToString(row["keyword"]);
                        model.Abstracts = Convert.ToString(row["abstracts"]);

                        model.Content = Convert.ToString(row["Content"]);

                        if (!Int32.TryParse(row["feedback"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Feedback = i;
                        if (!Int32.TryParse(row["sign"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Sign = i;
                        model.BrowseRight = Convert.ToString(row["browseRight"]);

                        if (!Int32.TryParse(row["browsetime"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.BrowseTime = i ;
                        DateTime.TryParse(row["releaseDate"].ToString(), out dt);
                        model.ReleaseDate = dt;

                        Int32.TryParse(row["integral"].ToString(), out i);
                        model.Integral = i;

                        if (!Int32.TryParse(row["TuiJian"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.TuiJian = i;

                        if (!Int32.TryParse(row["TiaoZhuan"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.TiaoZhuan = i;
                        if (!Int32.TryParse(row["JiaCu"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.JiaCu = i;

                        if (!Int32.TryParse(row["TouTiao"].ToString(), out i))
                        {
                            i = 0;
                        }

                        if (!Int32.TryParse(row["state"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.State = i;
                        model.StateName = i == 0 ? "待审核" : i == 1 ? "已审核" : "已删除";
                        model.UserRealName = Convert.ToString(row["realName"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }
        public LinkedList<NewsModel> GetViewNewsForWeb(int pageIndex, int pageSize, String conditions, String orderBy)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            sql.Append("Select a.* from tbl_news as a left join tbl_news_state as b on a.id=b.newsid ");
            sql.Append(" where b.state=1  ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(conditions + " ");
            }
            if (!String.IsNullOrEmpty(orderBy))
            {
                sql.Append(" Order By " + orderBy + "  limit ");
            }
            sql.Append((pageIndex - 1) * pageSize)
            .Append(",")
            .Append(pageSize);
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            LinkedList<NewsModel> list = new LinkedList<NewsModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        DateTime dt = DateTime.Now;
                        Int32 i;
                        Decimal d;
                        NewsModel model = new NewsModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Title = Convert.ToString(row["title"]);
                        model.ABBTitle = Convert.ToString(row["ABBTitle"]);
                        if (!Int32.TryParse(row["Types"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Types = i;
                        model.Source = Convert.ToString(row["source"]);
                        model.Author = Convert.ToString(row["author"]);
                        model.Target = Convert.ToString(row["target"]);
                        if (!Int32.TryParse(row["taxis"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Taxis = i;
                        model.AbbPic = Convert.ToString(row["abbPic"]);
                        model.Keyword = Convert.ToString(row["keyword"]);
                        model.Abstracts = Convert.ToString(row["abstracts"]);

                        model.Content = Convert.ToString(row["Content"]);

                        if (!Int32.TryParse(row["feedback"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Feedback = i;
                        if (!Int32.TryParse(row["sign"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.Sign = i;
                        model.BrowseRight = Convert.ToString(row["browseRight"]);

                        if (!Int32.TryParse(row["browsetime"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.BrowseTime = i;
                        DateTime.TryParse(row["releaseDate"].ToString(), out dt);
                        model.ReleaseDate = dt;

                        Int32.TryParse(row["integral"].ToString(), out i);
                        model.Integral = i;

                        if (!Int32.TryParse(row["TuiJian"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.TuiJian = i;

                        if (!Int32.TryParse(row["TiaoZhuan"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.TiaoZhuan = i;
                        if (!Int32.TryParse(row["JiaCu"].ToString(), out i))
                        {
                            i = 0;
                        }
                        model.JiaCu = i;

                        if (!Int32.TryParse(row["TouTiao"].ToString(), out i))
                        {
                            i = 0;
                        }

                      
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }
        public int GetNewsCount(String conditions)
        {
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select count(a.Id) From tbl_news as a left join tbl_news_state as b on a.id=b.newsid");
            sql.Append(" inner join tbl_userinfo as c  on a.addUserId=c.userid ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(" Where " + conditions);
            }
            #endregion

            Object obj = MysqlHelper.ExecuteScalar(conn, sql.ToString());
            return obj == null ? 0 : Convert.ToInt32(obj);

            #endregion
        }
        public int GetNewsCountForWeb(String conditions)
        {
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select count(a.Id) From tbl_news as a left join tbl_news_state as b on a.id=b.newsid");
            sql.Append(" where b.state=1  ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(conditions + " ");
            }
            #endregion

            Object obj = MysqlHelper.ExecuteScalar(conn, sql.ToString());
            return obj == null ? 0 : Convert.ToInt32(obj);

            #endregion
        }

        /// <summary>
        /// 获取某个分类所有文章的标签
        /// </summary>
        /// <param name="classid">分类id</param>
        /// <returns></returns>
        public IList<string> GetTargetList(int classid,string roleid)
        {
            IList<string> list=new List<string>();
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "select Target from tbl_news where Types=" + classid + " and BrowseRight like '%"+roleid+"%' order by id desc";
            DataTable dt = MySqlHelper.ExecuteDataset(conn, sql).Tables[0];
            string target = "";
            foreach (DataRow row in dt.Rows)
            {
                target = row["Target"].ToString();
                if(target!="")
                {
                    string[] tmpArr = target.Split(' ');
                    foreach (var s in tmpArr)
                    {
                        list.Add(s);
                    }
                }
            }
            return list;
        }
    }
}
